package com.zsc.shixun.controller;


import com.zsc.shixun.common.CommonResult;
import com.zsc.shixun.common.ResultUtil;
import com.zsc.shixun.model.notice.NoticeCreateVO;
import com.zsc.shixun.model.notice.NoticeUpdateVO;
import com.zsc.shixun.model.notice.NoticeVO;
import com.zsc.shixun.service.INoticeService;
import com.zsc.shixun.utils.LoginMessageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 公告管理 前端控制器
 * </p>
 *
 * @author ZWYZY
 * @since 2020-06-07
 */
@Controller
@RequestMapping("/notice")
@Validated
public class NoticeController {
    private static final Logger logger = LoggerFactory.getLogger(NoticeController.class);
    @Autowired
    private INoticeService noticeService;
    @Autowired
    private LoginMessageUtils loginMessageUtils;


    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        List<NoticeVO> noticeVOS = noticeService.getAll();
        request.setAttribute("noticeVOS", noticeVOS);
        return "admin/notice/index";
    }

    /**
     * 获取所有公告
     *
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    @Secured(value = "ROLE_notice_select")
    public CommonResult getData() {
        List<NoticeVO> noticeVOS = noticeService.getAll();
        return ResultUtil.success(noticeVOS);
    }

    /**
     * 修改公共
     *
     * @param noticeUpdateVO
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    @Secured(value = "ROLE_notice_update")
    public CommonResult update(@Validated @ModelAttribute NoticeUpdateVO noticeUpdateVO) {
        noticeService.update(noticeUpdateVO);
        return ResultUtil.success("公告更改成功，亲");
    }

    /**
     * 发布公告
     *
     * @param noticeCreateVO
     * @return
     */
    @PostMapping("/insert")
    @ResponseBody
    @Secured(value = "ROLE_notice_insert")
    public CommonResult insert(@Validated @ModelAttribute NoticeCreateVO noticeCreateVO) {
        noticeService.insert(noticeCreateVO);
        return ResultUtil.success("公告发布成功哦，亲");
    }

    /**
     * 删除公告
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    @Secured(value = "ROLE_notice_delete")
    public CommonResult delete(@NotNull(message = "id不能为空") @RequestParam(required = false) Long id) {
        noticeService.delete(id);
        return ResultUtil.success("删除公告成功，亲");
    }
}
